/**
 * Created by lenovo on 2016/10/10.
 */
var websocket = null;
var WS_PATH = "ws://" + document.location.host;
//var IP = "ws://122.228.248.78:2012";
//var IP = "ws://124.232.154.81:2012";
var IP = "ws://124.232.154.81:4012";
//var IP = "ws://192.168.31.88:2012";
//var IP = "ws://192.168.100.9:2012";
function websocketConnect(add) {
    if (add === undefined) {
        add = IP;
    }
    var support = "MozWebSocket" in window ? "MozWebSocket" : ("WebSocket" in window ? "WebSocket" : null);
//判断当前浏览器是否支持WebSocket
    if (support) {
        websocket = new WebSocket(add);
    } else {
        alert("Your browser cannot support WebSocket");
    }

//连接发生错误的回调方法\
    websocket.onerror = function() {
        setMessageInnerHTML("WebSocket连接发生错误");
    };

//连接成功建立的回调方法
    websocket.onopen = function() {
        var json;
        if (window.loginpage === true && window.loginstate === 0) {
            //获取状态
            var guid = window.newGuid();
            json = {
                action: "getloginlist",
                msg: {
                    Name: guid
                }
            };
            websocket.send(JSON.stringify(json));
        }
        if (window.loginpage === true && window.loginstate === 1) {
            //登陆
            json = {
                action: "login",
                msg: {
                    Tel: $("#T_uid").val(),
                    CarNo: $("#T_validate").val(),
                    Name: $("#T_uid").val()
                }
            };
            websocket.send(JSON.stringify(json));
        }if (window.loginpage === true && window.loginstate === 2) {
            //切换websocket连接
            json = {
                action: "sessionuser",
                msg: {
                    Operation: window.user,
                    Tel: window.session
                }
            };
            websocket.send(JSON.stringify(json));
        }
    };

    //接收到消息的回调方法
    websocket.onmessage = function(event) {
        var data = JSON.parse(event.data);
        var operation, result;

        if (data.Operation) {
            operation = data.Operation;
        }
        if (data.Result) {
            result = data.Result;
        }

        if (result) {
            if (result.length < 5) {
                return;
            }
            var ans = result.substr(1, 1);
            var code = result.substr(2);
            if (ans === "1"){
                switch (code) {
                        //数据管理
                    case "019": //"数据库更新":
                            if (Checkfunction("ReadyTourdRoom")) {
                                window.ReadyTourdRoom();
                            }
                        break;
                    case "002": //"登录":
                        if (Checkfunction("LoadSuccess")) {
                            window.LoadSuccess(data.Remark, data.Tel);
                        }
                        break;
                    case "040": //"登录成功":
                        if (Checkfunction("locationjump")) {
                            window.locationjump(data.Remark, data.Tel);
                        }
                        break;
                    case "018": //"登录列表":
                        if (Checkfunction("LoginList")) {
                            window.LoginList(JSON.parse(data.Remark));
                            websocket.close();
                        }
                        break;
                    case "001": //"提示":
                        if (window.ismonitor)
                            window.closelayer();
                        window.layermsg(data.Remark);
                        break;
                    default:
                        break;
                    }
                } else {
                switch (code) {
//                    case "001": //"提示":
//                        window.layererr(data.Remark);
//                        break;
                        case "数据库更新":
                            if (Checkfunction("RoomSaveError")) {
                                window.RoomSaveError(data.Remark);
                            }
                            break;
                        case "登录":
                            if (Checkfunction("CloseLoginLoad")) {
                                window.CloseLoginLoad();
                            }
                            break;
                        default:
                            window.layererr(data.Remark);
                            break;
                    }
                }
            }
        }
//    };

//连接关闭的回调方法
    websocket.onclose = function() {
//        setMessageInnerHTML("WebSocket连接关闭");
    };

//监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function() {
        closeWebSocket();
    };

//关闭WebSocket连接
    function closeWebSocket() {
        if (typeof (window.leaveout) === "function") {
            window.leaveout();
        }
        websocket.close();
    }

//发送消息
    function send(message) {
        //var message = document.getElementById('text').value;
        websocket.send(message);
    }
}

function websocketsent(msg) {
    websocket.send(msg);
}

function Checkfunction(func) {
    var hasfun = false;
    var str = "if(typeof(window." + func + ") === \"function\"){";
    str += " hasfun = true";
    str += "}";
    eval(str);
    return hasfun;
}